﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

/// <summary>
///JTokenExt 的摘要说明
/// </summary>
public static class F10JTokenExt
{
    public static string J_String(this JToken jt, string name)
    {
        if (jt[name].Type == JTokenType.String)
        {
            return (string)jt[name];
        }
        else
        {
            return string.Empty;
        }
    }



    public static float J_Float(this JToken jt, string name, float f)
    {
        var t = jt[name].Type;

        switch (t)
        {
            case JTokenType.Float:

                return (float)jt[name];

            case JTokenType.String:

                var s = (string)jt[name];

                var ret = f;

                return float.TryParse(s, out ret) ? ret : f;

            default:

                return f;
        }
    }

    public static int J_Integer(this JToken jt, string name, int f)
    {
        var t = jt[name].Type;

        switch (t)
        {
            case JTokenType.Integer:

                return (Int32)jt[name];

            case JTokenType.String:

                var s = (string)jt[name];

                var ret = f;

                return int.TryParse(s, out ret) ? ret : f;

            default:

                return f;
        }
    }


    public static int? J_IntegerNull(this JToken jt, string name)
    {
        return jt.J_IntegerNull(name, null);
    }
    public static int? J_IntegerNull(this JToken jt, string name, int? f)
    {
        var t = jt[name].Type;

        var ret = 0;

        switch (t)
        {
            case JTokenType.Integer:

                return (int)jt[name];

            case JTokenType.String:

                var s = (string)jt[name];

                return int.TryParse(s, out ret) ? ret : f;

            default:

                return f;
        }
    }
    public static float? J_FloatNull(this JToken jt, string name)
    {
        return jt.J_FloatNull(name, null);
    }
    public static float? J_FloatNull(this JToken jt, string name, float? f)
    {
        var t = jt[name].Type;

        switch (t)
        {
            case JTokenType.Float:

                return (float?)jt[name];

            case JTokenType.String:

                var s = (string)jt[name];

                var ret = 0f;

                return float.TryParse(s, out ret) ? ret : f;

            default:

                return f;
        }
    }

    public static double J_Double(this JToken jt, string name, double f)
    {
        var t = jt[name].Type;

        var ret = f;

        switch (t)
        {
            case JTokenType.Float:

                var temp = jt[name];

                return double.TryParse(temp.ToString(), out ret) ? ret : f;

            case JTokenType.Integer:

                return double.TryParse(((int)jt[name]).ToString(), out ret) ? ret : f;

            case JTokenType.String:

                var s = (string)jt[name];

                return double.TryParse(s, out ret) ? ret : f;

            default:

                return f;
        }
    }
    public static double? J_DoubleNull(this JToken jt, string name)
    {
        return jt.J_DoubleNull(name, null);
    }
    public static double? J_DoubleNull(this JToken jt, string name, double? f)
    {
        var t = jt[name].Type;

        var ret = 0d;

        switch (t)
        {
            case JTokenType.Float:

                var temp = jt[name];

                return double.TryParse(temp.ToString(), out ret) ? ret : f;

            case JTokenType.Integer:

                return double.TryParse(((int)jt[name]).ToString(), out ret) ? ret : f;

            case JTokenType.String:

                var s = (string)jt[name];

                return double.TryParse(s, out ret) ? ret : f;

            default:

                return f;
        }
    }

    public static DateTime J_DateTime(this JToken jt, string name, DateTime dt)
    {
        var t = jt[name].Type;

        switch (t)
        {
            case JTokenType.String:

                var s = (string)jt[name];

                var ret = dt;

                return DateTime.TryParse(s, out ret) ? ret : dt;

            default:

                return dt;
        }
    }
    public static DateTime J_DateTime(this JToken jt, string name, string format, DateTime dt)
    {
        var t = jt[name].Type;

        switch (t)
        {
            case JTokenType.String:
                {
                    var s = (string)jt[name];

                    var ret = dt;

                    return DateTime.TryParseExact(s, format, null, System.Globalization.DateTimeStyles.None, out ret) ? ret : dt;
                }
            case JTokenType.Integer:
                {
                    var s = (Int32)jt[name];

                    var ret = dt;

                    return DateTime.TryParseExact(s.ToString(), format, null, System.Globalization.DateTimeStyles.None, out ret) ? ret : dt;
                }
            default:

                return dt;
        }
    }
    public static DateTime? J_DateTimeNull(this JToken jt, string name)
    {
        return jt.J_DateTimeNull(name, null);
    }
    public static DateTime? J_DateTimeNull(this JToken jt, string name, DateTime? dt)
    {
        var t = jt[name].Type;

        switch (t)
        {
            case JTokenType.String:

                var s = (string)jt[name];

                var ret = DateTime.MinValue;

                return DateTime.TryParse(s, out ret) ? ret : dt;

            default:

                return dt;
        }
    }
}